/**
 * Created by yangfan on 2016/8/31.
 */
define(['config/appregister',
  "business/services/constant",'cookie','prism'],function(app,constant,cookie){

    app.directive("uiVideo",function ($rootScope){
        return {
            scope:{
                conf:"=confUrl"
            },
            template:'<div class="prism-player" id="J_prismPlayer"></div>',
            link:function(scope,element,attrs){
                var player = new prismplayer({
                    id: "J_prismPlayer", // 容器id
                    source: scope.conf.source,
                    autoplay: true, // 自动播放
                    width: "100%", // 播放器宽度
                    height: "400px", // 播放器高度
                  //  cover: scope.conf.cover
                });

                //模拟取得视频源数据
                var videoSourceId = scope.conf.id;
                //刷新进度跟踪的时间间隔
                var refreshTrackInterval = 1000;
                //刷新Cookie进度的方法
                var refreshTrack = function() {
                    //alert("1");
                    if(player != null) {
                        var currentTime = player.getCurrentTime();
                        //以视频源ID为索引 为videoTack数组赋予时间
                        cookie.setVideoTrack(videoSourceId, currentTime);
                  //      console.log('currentTime=' + currentTime);
                  ////      console.log('videoTrack=' + cookie.getVideoTrack("videoTrack")[videoSourceId]);
                    }

                } ;

                //存储Cookie中的videoTrack对象
                var videoTrack = {} ;
                //先定义为空

                player.on("uiReady", function() {
                    //在初始化视频组件后赋值
                  //  console.log(player.getCurrentTime());
                    //获取Cookie中的视频进度 并在视频组件初始化后跳转进度
                    //console.log(getVideoTrack("videoTrack"));
                    var videoTrack = cookie.getVideoTrack("videoTrack");
                   // console.log(videoTrack);
                    var trackTime = null;
                    if(videoTrack != null) {
                        trackTime = videoTrack[videoSourceId];
                       // console.log(trackTime);
                        player.seek(trackTime);
                    }
                    //function轮流执行
                   var timer = setInterval(refreshTrack, refreshTrackInterval);
                    $rootScope.$on('$stateChangeStart', function() {
                        clearInterval(timer)
                    })
                });
            }
        }
    })

});
